﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using FiveMonth.FormWork.Common;
using FiveMonth.FormWork.Word;
using NPOI.OpenXmlFormats.Dml;
using NPOI.OpenXmlFormats.Wordprocessing;
using NPOI.XWPF.Extractor;
using NPOI.XWPF.UserModel;
using NUnit.Framework;
using CT_R = NPOI.OpenXmlFormats.Shared.CT_R;

namespace Tester
{
    public class TestWordTemplate
    {
        [Test]
        public void Test2()
        {
            var data = Mock.GetScore();
            var data2 = Mock.GetScore2();

            var mappers = new BookmarkInfos<ScoreList>();

            mappers.Add("title", (d, a) => d.Title);
            mappers.Add("author", (d, a) => d.Author);
            mappers.Add("createdate", (d, a) => d.CreateDate.ToString("yyyy年MM月dd日"));
            mappers.AddImage("sign", (d, a) => d.Sign, 100, 100);
            mappers.Add("loop_id", (d, a) => d.Students[a[0]].Id);
            mappers.Add("loop_name", (d, a) => d.Students[a[0]].Name);
            mappers.Add("loop_age", (d, a) => d.Students[a[0]].Age.ToString());
            mappers.Add("loop_sex", (d, a) => d.Students[a[0]].Sex);
            mappers.Add("loop_totalscore", (d, a) => d.Students[a[0]].TotalScore.ToString());
            mappers.Add("loop_avg", (d, a) => d.Students[a[0]].AvgScore.ToString("N1"));
            mappers.Add("loop_item_subject", (d, a) => d.Students[a[0]].Subjects[a[1]].Subject);
            mappers.Add("loop_item_score", (d, a) => d.Students[a[0]].Subjects[a[1]].Score.ToString());
            mappers.AddLoop("loop_", (d, a) => d.Students.Count);
            mappers.AddLoop("loop_item_", (d, a) => d.Students[a[0]].Subjects.Count);

            var result = Template.ApplyData("D:\\Documents\\分行成绩表模板.docx", "D:\\Documents\\分行成绩表.doc", data, mappers);
            Console.WriteLine(result);
            var result2 = Template.ApplyData("D:\\Documents\\分行成绩表模板.docx", "D:\\Documents\\分行成绩表2.doc", data2, mappers);
            Console.WriteLine(result2);
        }
        [Test]
        public void TestNPoi()
        {
            var data = Mock.GetScore();
            var data2 = Mock.GetScore2();

            var mappers = new BookmarkInfos<ScoreList>();

            mappers.Add("title", (d, a) => d.Title);
            mappers.Add("author", (d, a) => d.Author);
            mappers.Add("createdate", (d, a) => d.CreateDate.ToString("yyyy年MM月dd日"));
            mappers.AddImage("sign", (d, a) => d.Sign, 100, 100);
            mappers.Add("loop_id", (d, a) => d.Students[a[0]].Id);
            mappers.Add("loop_name", (d, a) => d.Students[a[0]].Name);
            mappers.Add("loop_age", (d, a) => d.Students[a[0]].Age.ToString());
            mappers.Add("loop_sex", (d, a) => d.Students[a[0]].Sex);
            mappers.Add("loop_totalscore", (d, a) => d.Students[a[0]].TotalScore.ToString());
            mappers.Add("loop_avg", (d, a) => d.Students[a[0]].AvgScore.ToString("N1"));
            mappers.Add("loop_item_subject", (d, a) => d.Students[a[0]].Subjects[a[1]].Subject);
            mappers.Add("loop_item_score", (d, a) => d.Students[a[0]].Subjects[a[1]].Score.ToString());
            mappers.AddLoop("loop_", (d, a) => d.Students.Count);
            mappers.AddLoop("loop_item_", (d, a) => d.Students[a[0]].Subjects.Count);

            var filepath = "D:\\Documents\\Template.Npoi\\分行成绩表模板.docx";
            var result = Template.ApplyData(filepath, "D:\\Documents\\Template.Npoi\\分行成绩表.docx", data, mappers);
            Console.WriteLine(result);
            //using (FileStream fs = File.OpenRead(filepath))
            //{
            //    XWPFDocument doc = new XWPFDocument(fs);
            //    var ctd = Reflaction.GetMemberValue<CT_Document>(doc, "Document");
            //    foreach (var item in ctd.body.Items)
            //    {
            //        if (item is CT_P)
            //        {
            //            CT_P ctp = item as CT_P;
            //            var bookmarks = ctp.GetBookmarkStartList();
            //            // Get all bookmarks and loop through them 
            //            foreach (var bookmark in bookmarks)
            //            {
            //                // Extract the name of the bookmark 
            //                var bookmarkName = bookmark.name;
            //                Console.WriteLine(bookmarkName);
            //            }
            //        }
            //        else if (item is CT_Tbl)
            //        {
            //            CT_Tbl ctp = item as CT_Tbl;
            //            //var bookmarks = ctp.();
            //            //// Get all bookmarks and loop through them 
            //            //foreach (var bookmark in bookmarks)
            //            //{
            //            //    // Extract the name of the bookmark 
            //            //    var bookmarkName = bookmark.name;
            //            //    Console.WriteLine(bookmarkName);
            //            //}
            //        }
            //    }
            //    //foreach (var para in doc.Paragraphs)
            //    //{
            //    //    CT_P ctp = Reflaction.Invoke<CT_P>(para, "GetCTP");
            //    //    var bookmarks = ctp.GetBookmarkStartList();
            //    //    // Get all bookmarks and loop through them 
            //    //    foreach (var bookmark in bookmarks)
            //    //    {
            //    //        // Extract the name of the bookmark 
            //    //        var bookmarkName = bookmark.name;
            //    //        Console.WriteLine(bookmarkName);
            //    //    }
            //    //}

            //    doc.Close();
        }
    }
}